/**
 * Server management
 */
var _servermgmtIp;
var _serverpubIp;
var server = {
	/**
	 * Show server list
	 */
	list : function() {
		var serverlistUrl = baseURL + "/monitor/server/list"
		$.ajax({
			type : "get",
			url : serverlistUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#content").empty().html(html);
			}
		});
	},
	

	serverinfo : function(element){
		var mgmtaddr = $(element).attr("mgmtaddr");
		var pubaddr = $(element).attr("pubaddr");
		_serverpubIp = pubaddr;
		_servermgmtIp = mgmtaddr;
		var title;
		$('#serverlist td[name="name"]').each(function(){
			if($(this).attr("value") == mgmtaddr){
				title = $(this).text();
			}
	     });  
		title = title;
		var param = {};
		param.addr = mgmtaddr;
		common.dialog({
			obj : "#showClusterInfo",
			url : baseURL + "/monitor/server/serverinfo",
			param : param,
			title : title,
			width : 650,
			height : 600,
			nobutton: true
		});
	},
	
	setPre : function(){
		var ip = _servermgmtIp;
		var cminfoUrl = baseURL + "/monitor/server/cmused"
		$.ajax({
			type : "get",
			url : cminfoUrl,
			dataTpe : "json",
			data : {ip : ip},
			beforeSend : function() {},
			success : function(json){
				var cpuvalue = json.CPU;
				var memvalue = json.mem;
				 $('#CPUPre').progressbar({
					 value : cpuvalue,
				 });;
				 $('#MemPre').progressbar({
					 value : memvalue,
				 });
			}
		});
	},
	
	status : function(){
		$('#serverlist td[name="status"]').each(function(){
			if($(this).attr("value") == 0){
				$(this).text(localeMsg.common.active);
			}
			else{
				 $(this).text(localeMsg.common.inactive);
			}
	     });
	},
	
	sync : function(){
		var serverlistUrl = baseURL + "/monitor/server/sync"
		$.ajax({
			type : "get",
			url : serverlistUrl,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.syncSuccess,
						callback : function() {
							server.list();
						}
					});
				} else {
					common.showTip({
						success : false,
						title : localeMsg.common.warn,
						text : localeMsg.common.syncFailed,
					});
				}
			}
		});
		
	},
	
 	//xueqiupeng 指定disk加入集群
 	addosd: function(element){
 		var diskNameValue = $(element).attr("diskname");
 		var serverIdValue = $(element).attr("mgmtAddr");
 		$.ajax({
 			type : "get",
 			url : window.location.pathname+"/addosd",
 			data : {diskName : diskNameValue,serverId : serverIdValue},
 			traditional: true,
 			dataType : "json",
 			success : function(resultInt) {
 				if (resultInt == 0) {
 					common.showTip({
 						title : localeMsg.common.prompt,
 						text : localeMsg.alarm.readSuccess,
 						width : 300,
 						height : 250,
						callback : function(html) {
							window.location.reload();
						}
 					});
 				} else {
 					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.operationFail});
 				}
 			}
 		});
 	},
 //按照diskId查询对应的分区	
 	partition:function(element){
// 		$("#p1").empty();
		var diskNameValue = $(element).attr("diskname");
		var serverIdValue = $(element).attr("serverid");
		var partitionlistUrl = baseURL + "/monitor/server/partitionInfo";
		var param = {};
		param.diskName = diskNameValue;
		param.serverId = serverIdValue;
		common.dialog({
			obj : "#p1",
			type : "get",
			param : param,
			url : partitionlistUrl,
			title : "分区信息",
			width : 400,
			nobutton: true,
		});
		
		
		
		
// 		$.ajax({
// 			type : "get",
// 			url : partitionlistUrl,
// 			data : {diskId : diskIdValue},
// 			traditional: true,
// 			dataType : "json",
// 			success : function(html) {
// 				$("#p1").empty();
//			}
// 		});
 	},
 	//休眠与启动节点
	dormancy:function(mgmtAddr,isDormant,obj){
		var operationStatusUrl = baseURL + "/monitor/server/operation"
		var json={mgmtAddr:mgmtAddr,isDormant:isDormant};
		$.ajax({
			type:"post",
			data:json,
			url:operationStatusUrl,
			success : function(result){
				if(result.result=='success'){
					if(isDormant==0){
						$(obj).removeClass("btn btn-primary btn-xs btn-danger");
						$(obj).addClass("btn btn-primary btn-xs btn-success")
						$(obj).attr("onclick","server.operation('"+result.server.mgmtAddr+"','"+result.server.isDormant+"',this);");
						$(obj).text(localeMsg.common.disabled);
					}else{
						$(obj).removeClass("btn btn-primary btn-xs btn-success");
						$(obj).addClass("btn btn-primary btn-xs btn-danger")
						$(obj).attr("onclick","server.operation('"+result.server.mgmtAddr+"','"+result.server.isDormant+"',this);");
						$(obj).text(localeMsg.common.dormancy);
					}
				}else{
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.operationFail,
					});
				}
				
			}
		});
	},
	operation:function(mgmtAddr,isDormant,obj){
		if(isDormant==0){
			var title=localeMsg.common.dormancyTime
			common.dialog({
				obj : "#editServerDialog",
				type : "get",
				url : baseURL + "/monitor/server/edit",
				title : title,
				width : 400,
				confirm : function() {
					var time=$("#val").val();
					if(time!=0){
						var statusJson={mgmtAddr:mgmtAddr,isDormant:isDormant};
						$.ajax({
							type:"GET",
							data:statusJson,
							url:baseURL+"/monitor/server/updateWaitRestore",
							success : function(result){
								if(result.result=='success'){
									$(obj).removeClass("btn btn-primary btn-xs btn-danger");
									$(obj).addClass("btn btn-primary btn-xs btn-success")
									$(obj).attr("onclick","server.operation('"+result.server.mgmtAddr+"','"+result.server.isDormant+"',this);");
									$(obj).text(localeMsg.common.disabled);
									var json={mgmtAddr:mgmtAddr,time:time};
									$.ajax({
										type:"GET",
										data:json,
										async:true,
										url:baseURL+"/monitor/server/restoreNode"
									});
								}
							}
						});
					}else{
						server.dormancy(mgmtAddr,isDormant,obj);
					}
				},
			});
		}else if(isDormant==3){
			common.dialog({
				obj : "#waitServerDialog",
				type : "get",
				url : baseURL + "/monitor/server/waitRestore",
				title : localeMsg.common.prompt,
				width : 400,
				confirm : function() {
					server.dormancy(mgmtAddr,isDormant,obj);
				},
			});
		}else{
			server.dormancy(mgmtAddr,isDormant,obj);
		}
	},
	/**
	 * validate form
	 */
	validate : function() {
		$('#val').validatebox({
			required : true,
			validType : ['digits']
		});
	},
	dormant:function(){
		$("#serverlist td[name='isDormant']").each(function(){
			if($(this).attr("value") == 2){
				common.showTip({
					title : localeMsg.common.warn,
					text : localeMsg.common.restoreNodeFail,
				});
			}
	     });
		
	}
};